// 视觉效果增强系统 - 屏幕震动、粒子效果、伤害数字等
class VisualEffectsSystem {
    constructor() {
        this.canvas = document.getElementById('gameCanvas');
        this.ctx = this.canvas.getContext('2d');
        
        // 屏幕震动
        this.screenShake = {
            intensity: 0,
            duration: 0,
            offsetX: 0,
            offsetY: 0
        };
        
        // 伤害数字
        this.damageNumbers = [];
        
        // 连击系统
        this.killStreak = 0;
        this.lastKillTime = 0;
        this.streakResetTime = 3000; // 3秒内没有击杀则重置连击
        
        // 强化粒子效果
        this.enhancedParticles = [];
        
        // 慢动作效果
        this.slowMotion = {
            active: false,
            factor: 1,
            duration: 0
        };
        
        this.init();
    }
    
    init() {
        // 监听游戏事件
        this.setupEventListeners();
        
        // 启动更新循环
        this.startUpdateLoop();
    }
    
    setupEventListeners() {
        // 这里可以添加自定义事件监听
        document.addEventListener('enemyKilled', (e) => {
            this.onEnemyKilled(e.detail);
        });
        
        document.addEventListener('playerHit', (e) => {
            this.onPlayerHit(e.detail);
        });
        
        document.addEventListener('weaponFired', (e) => {
            this.onWeaponFired(e.detail);
        });
    }
    
    startUpdateLoop() {
        const update = () => {
            this.update();
            requestAnimationFrame(update);
        };
        update();
    }
    
    update() {
        this.updateScreenShake();
        this.updateDamageNumbers();
        this.updateKillStreak();
        this.updateSlowMotion();
        this.updateEnhancedParticles();
    }
    
    // 屏幕震动系统
    startScreenShake(intensity = 5, duration = 300) {
        // 震动效果已禁用
        return;
    }
    
    // 创建屏幕闪烁效果 - 已禁用全屏闪光，改为局部效果
    createScreenFlash(intensity) {
        // 不再创建全屏闪光效果，改为局部小范围闪光
        // 这样可以避免玩家看不见怪物的问题
        
        // 可选：创建小范围的局部闪光效果
        if (intensity > 10) {
            // 只有在非常高强度时才创建小范围闪光
            this.createLocalFlash(intensity);
        }
        
        // 不再添加全屏闪光样式
        console.log('屏幕闪光效果已禁用，强度:', intensity);
    }
    
    // 创建局部闪光效果 - 替代全屏闪光
    createLocalFlash(intensity) {
        const flash = document.createElement('div');
        
        flash.style.cssText = `
            position: fixed;
            top: 50%;
            left: 50%;
            width: 100px;
            height: 100px;
            background: radial-gradient(circle, rgba(255, 255, 255, 0.3), transparent);
            border-radius: 50%;
            transform: translate(-50%, -50%);
            z-index: 9998;
            pointer-events: none;
            animation: localFlashFade 0.2s ease-out;
        `;
        
        document.body.appendChild(flash);
        
        // 添加局部闪光样式
        if (!document.getElementById('localFlashStyles')) {
            const styles = document.createElement('style');
            styles.id = 'localFlashStyles';
            styles.textContent = `
                @keyframes localFlashFade {
                    0% { opacity: 0.8; transform: translate(-50%, -50%) scale(0.5); }
                    50% { opacity: 0.4; transform: translate(-50%, -50%) scale(1.5); }
                    100% { opacity: 0; transform: translate(-50%, -50%) scale(2); }
                }
            `;
            document.head.appendChild(styles);
        }
        
        setTimeout(() => {
            if (flash.parentNode) {
                document.body.removeChild(flash);
            }
        }, 200);
    }
    
    // 获取闪烁类型
    getFlashType(intensity) {
        if (intensity > 12) {
            return {
                gradient: `radial-gradient(circle, rgba(255, 100, 100, ${intensity * 0.08}), rgba(255, 150, 0, ${intensity * 0.04}), transparent 60%)`,
                animation: 'explosionFlash',
                duration: 0.3,
                blendMode: 'screen'
            };
        } else if (intensity > 8) {
            return {
                gradient: `radial-gradient(circle, rgba(255, 200, 100, ${intensity * 0.06}), rgba(255, 255, 255, ${intensity * 0.03}), transparent 70%)`,
                animation: 'criticalFlash',
                duration: 0.2,
                blendMode: 'overlay'
            };
        } else if (intensity > 4) {
            return {
                gradient: `radial-gradient(circle, rgba(255, 80, 80, ${intensity * 0.07}), transparent 80%)`,
                animation: 'damageFlash',
                duration: 0.15,
                blendMode: 'multiply'
            };
        } else {
            return {
                gradient: `radial-gradient(circle, rgba(255, 255, 255, ${intensity * 0.05}), transparent 70%)`,
                animation: 'flashFade',
                duration: 0.1,
                blendMode: 'normal'
            };
        }
    }
    
    updateScreenShake() {
        if (this.screenShake.duration > 0) {
            this.screenShake.duration -= 16; // 约60fps
            
            const shakeAmount = this.screenShake.intensity * (this.screenShake.duration / 300);
            this.screenShake.offsetX = (Math.random() - 0.5) * shakeAmount;
            this.screenShake.offsetY = (Math.random() - 0.5) * shakeAmount;
            
            // 应用震动到画布
            this.applyScreenShake();
        } else {
            this.screenShake.offsetX = 0;
            this.screenShake.offsetY = 0;
        }
    }
    
    applyScreenShake() {
        if (this.canvas) {
            this.canvas.style.transform = `translate(${this.screenShake.offsetX}px, ${this.screenShake.offsetY}px)`;
        }
    }
    
    // 伤害数字系统
    showDamageNumber(x, y, damage, isCritical = false, color = '#ff6b6b') {
        this.damageNumbers.push({
            x: x,
            y: y,
            damage: damage,
            isCritical: isCritical,
            color: color,
            life: 1000, // 1秒生命周期
            maxLife: 1000,
            velocityY: -50,
            scale: isCritical ? 1.5 : 1,
            alpha: 1
        });
    }
    
    updateDamageNumbers() {
        this.damageNumbers.forEach((dmgNum, index) => {
            dmgNum.life -= 16;
            dmgNum.y += dmgNum.velocityY * 0.016;
            dmgNum.velocityY += 30; // 重力
            dmgNum.alpha = dmgNum.life / dmgNum.maxLife;
            
            if (dmgNum.life <= 0) {
                this.damageNumbers.splice(index, 1);
            }
        });
    }
    
    renderDamageNumbers(ctx) {
        this.damageNumbers.forEach(dmgNum => {
            ctx.save();
            ctx.globalAlpha = dmgNum.alpha;
            ctx.fillStyle = dmgNum.color;
            ctx.font = `bold ${16 * dmgNum.scale}px Arial`;
            ctx.textAlign = 'center';
            ctx.strokeStyle = '#000';
            ctx.lineWidth = 2;
            
            if (dmgNum.isCritical) {
                ctx.strokeText(dmgNum.damage.toString(), dmgNum.x, dmgNum.y);
            }
            
            ctx.fillText(dmgNum.damage.toString(), dmgNum.x, dmgNum.y);
            ctx.restore();
        });
    }
    
    // 连击系统
    addKill() {
        const now = Date.now();
        
        if (now - this.lastKillTime < this.streakResetTime) {
            this.killStreak++;
        } else {
            this.killStreak = 1;
        }
        
        this.lastKillTime = now;
        
        // 连击特效
        this.showKillStreakEffect();
        
        // 连击越高震动越强 - 但限制最大强度避免过度干扰
        const shakeIntensity = Math.min(this.killStreak * 1, 6); // 减少震动强度
        // this.startScreenShake(shakeIntensity, 150); // 禁用震动时间
    }
    
    updateKillStreak() {
        if (Date.now() - this.lastKillTime > this.streakResetTime) {
            this.killStreak = 0;
        }
    }
    
    showKillStreakEffect() {
        if (this.killStreak <= 1) return;
        
        const canvas = document.getElementById('gameCanvas');
        const centerX = canvas.width / 2;
        const centerY = canvas.height / 2;
        
        // 创建连击文字效果
        const streakText = document.createElement('div');
        streakText.className = 'kill-streak-text';
        streakText.textContent = `${this.killStreak}x KILL STREAK!`;
        streakText.style.cssText = `
            position: fixed;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: ${Math.min(20 + this.killStreak * 2, 40)}px;
            font-weight: bold;
            color: #ff6b6b;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
            z-index: 9999;
            pointer-events: none;
            animation: killStreakPop 1s ease-out;
        `;
        
        document.body.appendChild(streakText);
        
        // 添加连击动画样式
        if (!document.getElementById('killStreakStyles')) {
            const styles = document.createElement('style');
            styles.id = 'killStreakStyles';
            styles.textContent = `
                @keyframes killStreakPop {
                    0% { 
                        opacity: 0;
                        transform: translate(-50%, -50%) scale(0.5);
                    }
                    50% {
                        opacity: 1;
                        transform: translate(-50%, -50%) scale(1.2);
                    }
                    100% { 
                        opacity: 0;
                        transform: translate(-50%, -50%) scale(1);
                    }
                }
            `;
            document.head.appendChild(styles);
        }
        
        // 1秒后移除
        setTimeout(() => {
            if (streakText.parentNode) {
                document.body.removeChild(streakText);
            }
        }, 1000);
        
        // 高连击时的特殊效果
        if (this.killStreak >= 10) {
            this.activateSlowMotion(0.5, 1000);
        }
    }
    
    // 慢动作效果
    activateSlowMotion(factor = 0.5, duration = 1000) {
        this.slowMotion.active = true;
        this.slowMotion.factor = factor;
        this.slowMotion.duration = duration;
    }
    
    updateSlowMotion() {
        if (this.slowMotion.active) {
            this.slowMotion.duration -= 16;
            
            if (this.slowMotion.duration <= 0) {
                this.slowMotion.active = false;
                this.slowMotion.factor = 1;
            }
        }
    }
    
    getSlowMotionFactor() {
        return this.slowMotion.active ? this.slowMotion.factor : 1;
    }
    
    // 强化粒子效果 - 血迹效果已禁用
    createBloodExplosion(x, y, intensity = 1) {
        // 血迹效果已禁用
        return;
    }
    
    // 创建血液溅射线条效果 - 血迹效果已禁用
    createBloodSplatter(x, y, intensity) {
        // 血迹效果已禁用
        return;
    }
    
    // 创建血液滴落效果 - 血迹效果已禁用
    createBloodDrops(x, y, intensity) {
        // 血迹效果已禁用
        return;
    }
    
    createMuzzleFlash(x, y, angle) {
        const flashCount = 8;
        
        for (let i = 0; i < flashCount; i++) {
            const spread = Math.PI / 4; // 45度扩散
            const particleAngle = angle + (Math.random() - 0.5) * spread;
            
            this.enhancedParticles.push({
                x: x,
                y: y,
                vx: Math.cos(particleAngle) * (100 + Math.random() * 50),
                vy: Math.sin(particleAngle) * (100 + Math.random() * 50),
                life: 200 + Math.random() * 100,
                maxLife: 200 + Math.random() * 100,
                size: Math.random() * 3 + 1,
                color: this.getRandomMuzzleColor(),
                gravity: 0
            });
        }
    }
    
    createExplosion(x, y, radius = 50) {
        const particleCount = 25;
        
        for (let i = 0; i < particleCount; i++) {
            const angle = (Math.PI * 2 / particleCount) * i;
            const speed = 50 + Math.random() * 100;
            
            this.enhancedParticles.push({
                x: x,
                y: y,
                vx: Math.cos(angle) * speed,
                vy: Math.sin(angle) * speed,
                life: 1500 + Math.random() * 500,
                maxLife: 1500 + Math.random() * 500,
                size: Math.random() * 6 + 3,
                color: this.getRandomExplosionColor(),
                gravity: 30
            });
        }
    }
    
    updateEnhancedParticles() {
        this.enhancedParticles.forEach((particle, index) => {
            particle.life -= 16;
            particle.x += particle.vx * 0.016;
            particle.y += particle.vy * 0.016;
            particle.vy += particle.gravity * 0.016;
            
            // 更新旋转
            if (particle.rotation !== undefined) {
                particle.rotation += particle.rotationSpeed;
            }
            
            // 更新透明度
            if (particle.alpha !== undefined && particle.fade) {
                particle.alpha -= particle.fade;
                if (particle.alpha <= 0) {
                    particle.alpha = 0;
                }
            }
            
            // 添加空气阻力
            if (particle.type !== 'line') {
                particle.vx *= 0.98;
                particle.vy *= 0.98;
            }
            
            // 特殊粒子类型处理
            if (particle.type === 'drop') {
                // 血液滴落的特殊物理
                if (particle.vy > 200) {
                    particle.vy = 200; // 限制最大下落速度
                }
                
                // 模拟碰撞地面
                if (particle.y > 700) { // 假设画布高度为750
                    particle.vy *= -0.3; // 弹起但损失大部分能量
                    particle.y = 700;
                    particle.vx *= 0.7; // 横向摩擦
                }
            }
            
            if (particle.life <= 0 || (particle.alpha !== undefined && particle.alpha <= 0)) {
                this.enhancedParticles.splice(index, 1);
            }
        });
    }
    
    renderEnhancedParticles(ctx) {
        this.enhancedParticles.forEach(particle => {
            ctx.save();
            const alpha = particle.alpha || (particle.life / particle.maxLife);
            ctx.globalAlpha = alpha;
            
            if (particle.type === 'line') {
                // 渲染线条效果
                ctx.strokeStyle = particle.color;
                ctx.lineWidth = particle.size;
                ctx.lineCap = 'round';
                ctx.shadowColor = particle.color;
                ctx.shadowBlur = particle.size;
                ctx.beginPath();
                ctx.moveTo(particle.x, particle.y);
                ctx.lineTo(particle.x + particle.vx * 0.1, particle.y + particle.vy * 0.1);
                ctx.stroke();
            } else if (particle.type === 'drop') {
                // 渲染血液滴落
                ctx.fillStyle = particle.color;
                ctx.shadowColor = particle.color;
                ctx.shadowBlur = particle.size * 0.5;
                
                ctx.translate(particle.x, particle.y);
                ctx.beginPath();
                ctx.arc(0, 0, particle.size / 2, 0, Math.PI * 2);
                ctx.fill();
                
                // 添加尾迹效果
                ctx.globalAlpha = alpha * 0.5;
                ctx.beginPath();
                ctx.ellipse(0, particle.size / 4, particle.size / 4, particle.size / 2, 0, 0, Math.PI * 2);
                ctx.fill();
            } else if (particle.type === 'critical') {
                // 渲染暴击粒子
                ctx.fillStyle = particle.color;
                ctx.translate(particle.x, particle.y);
                
                if (particle.rotation !== undefined) {
                    ctx.rotate(particle.rotation);
                }
                
                // 增强的发光效果
                ctx.shadowColor = particle.color;
                ctx.shadowBlur = particle.size * 3;
                
                // 渲染带青光的粒子
                ctx.fillRect(
                    -particle.size / 2,
                    -particle.size / 2,
                    particle.size,
                    particle.size
                );
                
                // 添加强烈的高光效果
                ctx.globalAlpha = alpha * 0.8;
                ctx.fillStyle = '#ffffff';
                ctx.fillRect(
                    -particle.size / 3,
                    -particle.size / 3,
                    particle.size / 1.5,
                    particle.size / 1.5
                );
            } else if (particle.type === 'shockwave') {
                // 渲染冲击波粒子
                ctx.fillStyle = particle.color;
                ctx.translate(particle.x, particle.y);
                
                if (particle.rotation !== undefined) {
                    ctx.rotate(particle.rotation);
                }
                
                // 冲击波特效
                ctx.shadowColor = particle.color;
                ctx.shadowBlur = particle.size * 2;
                
                // 渲染圆形粒子
                ctx.beginPath();
                ctx.arc(0, 0, particle.size / 2, 0, Math.PI * 2);
                ctx.fill();
            } else {
                // 渲染普通粒子
                ctx.fillStyle = particle.color;
                ctx.translate(particle.x, particle.y);
                
                if (particle.rotation !== undefined) {
                    ctx.rotate(particle.rotation);
                }
                
                // 添加发光效果
                ctx.shadowColor = particle.color;
                ctx.shadowBlur = particle.size * 2;
                
                // 渲染方形粒子
                ctx.fillRect(
                    -particle.size / 2,
                    -particle.size / 2,
                    particle.size,
                    particle.size
                );
                
                // 添加高光效果
                ctx.globalAlpha = alpha * 0.6;
                ctx.fillStyle = '#ffffff';
                ctx.fillRect(
                    -particle.size / 4,
                    -particle.size / 4,
                    particle.size / 2,
                    particle.size / 2
                );
            }
            
            ctx.restore();
        });
    }
    
    // 颜色辅助方法
    getRandomBloodColor() {
        const colors = ['#8B0000', '#DC143C', '#B22222', '#FF0000'];
        return colors[Math.floor(Math.random() * colors.length)];
    }
    
    getRandomMuzzleColor() {
        const colors = ['#FFD700', '#FFA500', '#FF4500', '#FFFFFF'];
        return colors[Math.floor(Math.random() * colors.length)];
    }
    
    getRandomExplosionColor() {
        const colors = ['#FF8C00', '#FF4500', '#FF0000', '#FFD700'];
        return colors[Math.floor(Math.random() * colors.length)];
    }
    
    // 事件处理
    onEnemyKilled(data) {
        this.addKill();
        // this.createBloodExplosion(data.x, data.y, 1.5); // 禁用血迹效果
        this.showDamageNumber(data.x, data.y - 20, data.damage, data.isCritical, '#ff6b6b');
        
        // 减少震动强度
        // this.startScreenShake(4, 100); // 禁用震动效果
        
        // 添加击杀特效 - 已禁用红色特效
        // this.createKillEffect(data.x, data.y, data.isCritical);
    }
    
    // 创建击杀特效 - 已禁用红色圆圈和文字特效
    createKillEffect(x, y, isCritical = false) {
        // 禁用击杀特效 - 不再创建红色文字效果
        return;
        // 创建击杀文字效果
        const killText = document.createElement('div');
        killText.className = 'kill-effect-text';
        killText.textContent = isCritical ? 'CRITICAL KILL!' : 'KILL!';
        killText.style.cssText = `
            position: fixed;
            left: ${x}px;
            top: ${y}px;
            transform: translate(-50%, -50%);
            font-size: ${isCritical ? '28px' : '20px'};
            font-weight: bold;
            color: ${isCritical ? '#ff0000' : '#ff6b6b'};
            text-shadow: 
                2px 2px 4px rgba(0,0,0,0.8),
                0 0 10px ${isCritical ? 'rgba(255, 0, 0, 0.8)' : 'rgba(255, 107, 107, 0.6)'};
            z-index: 9999;
            pointer-events: none;
            animation: ${isCritical ? 'criticalKillPop' : 'killPop'} 1s ease-out;
            font-family: 'Impact', 'Arial Black', sans-serif;
            letter-spacing: 1px;
        `;
        
        document.body.appendChild(killText);
        
        // 添加击杀动画样式
        if (!document.getElementById('killEffectStyles')) {
            const styles = document.createElement('style');
            styles.id = 'killEffectStyles';
            styles.textContent = `
                @keyframes killPop {
                    0% { 
                        opacity: 0;
                        transform: translate(-50%, -50%) scale(0.5) rotate(-10deg);
                    }
                    50% {
                        opacity: 1;
                        transform: translate(-50%, -50%) scale(1.2) rotate(5deg);
                    }
                    100% { 
                        opacity: 0;
                        transform: translate(-50%, -50%) scale(1) rotate(0deg);
                    }
                }
                @keyframes criticalKillPop {
                    0% { 
                        opacity: 0;
                        transform: translate(-50%, -50%) scale(0.3) rotate(-20deg);
                    }
                    30% {
                        opacity: 1;
                        transform: translate(-50%, -50%) scale(1.5) rotate(10deg);
                    }
                    60% {
                        opacity: 0.8;
                        transform: translate(-50%, -50%) scale(1.3) rotate(-5deg);
                    }
                    100% { 
                        opacity: 0;
                        transform: translate(-50%, -50%) scale(1) rotate(0deg);
                    }
                }
            `;
            document.head.appendChild(styles);
        }
        
        // 移除文字效果
        setTimeout(() => {
            if (killText.parentNode) {
                document.body.removeChild(killText);
            }
        }, 800);
        
        // 如果是暴击，添加额外的爆炸效果
        // if (isCritical) {
        //     this.createCriticalExplosion(x, y); // 禁用暴击爆炸效果
        // }
    }
    
    // 创建暴击爆炸效果
    createCriticalExplosion(x, y) {
        const particleCount = 30;
        
        for (let i = 0; i < particleCount; i++) {
            const angle = (Math.PI * 2 / particleCount) * i;
            const speed = 150 + Math.random() * 100;
            
            this.enhancedParticles.push({
                x: x,
                y: y,
                vx: Math.cos(angle) * speed,
                vy: Math.sin(angle) * speed,
                life: 1000,
                maxLife: 1000,
                size: Math.random() * 5 + 3,
                color: this.getRandomCriticalColor(),
                gravity: 50,
                rotation: Math.random() * Math.PI * 2,
                rotationSpeed: (Math.random() - 0.5) * 0.3,
                alpha: 1,
                fade: 0.002
            });
        }
    }
    
    // 获取随机暴击颜色
    getRandomCriticalColor() {
        const colors = ['#ff0000', '#ff4500', '#ffd700', '#ffffff', '#ffff00'];
        return colors[Math.floor(Math.random() * colors.length)];
    }
    
    onPlayerHit(data) {
        // this.startScreenShake(5, 200); // 禁用受伤震动
        this.showDamageNumber(data.x, data.y - 20, data.damage, false, '#ff0000');
        
        // 血液效果
        // this.createBloodExplosion(data.x, data.y, 0.8); // 禁用血迹效果
        
        // 添加受伤特效
        // this.createDamageEffect(data.x, data.y, data.damage); // 禁用受伤特效
    }
    
    // 创建受伤特效
    createDamageEffect(x, y, damage) {
        // 创建受伤文字效果
        const damageText = document.createElement('div');
        damageText.className = 'damage-effect-text';
        damageText.textContent = `-${damage}`;
        damageText.style.cssText = `
            position: fixed;
            left: ${x}px;
            top: ${y}px;
            transform: translate(-50%, -50%);
            font-size: 20px;
            font-weight: bold;
            color: #ff0000;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
            z-index: 9999;
            pointer-events: none;
            animation: damagePop 1s ease-out;
        `;
        
        document.body.appendChild(damageText);
        
        // 添加受伤动画样式
        if (!document.getElementById('damageEffectStyles')) {
            const styles = document.createElement('style');
            styles.id = 'damageEffectStyles';
            styles.textContent = `
                @keyframes damagePop {
                    0% { 
                        opacity: 0;
                        transform: translate(-50%, -50%) scale(0.5) translateY(0px);
                    }
                    30% {
                        opacity: 1;
                        transform: translate(-50%, -50%) scale(1.3) translateY(-10px);
                    }
                    100% { 
                        opacity: 0;
                        transform: translate(-50%, -50%) scale(1) translateY(-30px);
                    }
                }
            `;
            document.head.appendChild(styles);
        }
        
        // 移除文字效果
        setTimeout(() => {
            if (damageText.parentNode) {
                document.body.removeChild(damageText);
            }
        }, 1000);
        
        // 创建受伤波纹效果
        this.createDamageRipple(x, y);
    }
    
    // 创建受伤波纹效果
    createDamageRipple(x, y) {
        const ripple = document.createElement('div');
        ripple.className = 'damage-ripple';
        ripple.style.cssText = `
            position: fixed;
            left: ${x}px;
            top: ${y}px;
            width: 0;
            height: 0;
            border: 3px solid rgba(255, 0, 0, 0.7);
            border-radius: 50%;
            transform: translate(-50%, -50%);
            z-index: 9998;
            pointer-events: none;
            animation: rippleExpand 0.6s ease-out;
        `;
        
        document.body.appendChild(ripple);
        
        // 添加波纹动画样式
        if (!document.getElementById('rippleStyles')) {
            const styles = document.createElement('style');
            styles.id = 'rippleStyles';
            styles.textContent = `
                @keyframes rippleExpand {
                    0% { 
                        width: 0;
                        height: 0;
                        opacity: 1;
                    }
                    100% { 
                        width: 200px;
                        height: 200px;
                        opacity: 0;
                    }
                }
            `;
            document.head.appendChild(styles);
        }
        
        // 移除波纹效果
        setTimeout(() => {
            if (ripple.parentNode) {
                document.body.removeChild(ripple);
            }
        }, 600);
    }
    
    onWeaponFired(data) {
        this.createMuzzleFlash(data.x, data.y, data.angle);
        // this.startScreenShake(2, 50); // 禁用武器射击震动
        
        // 添加开火特效
        this.createFireEffect(data.x, data.y, data.angle);
    }
    
    // 创建开火特效
    createFireEffect(x, y, angle) {
        // 创建开火闪光
        const flash = document.createElement('div');
        flash.className = 'muzzle-flash';
        flash.style.cssText = `
            position: fixed;
            left: ${x}px;
            top: ${y}px;
            width: 20px;
            height: 20px;
            background: radial-gradient(circle, rgba(255, 255, 255, 0.9), rgba(255, 200, 100, 0.7), transparent);
            border-radius: 50%;
            transform: translate(-50%, -50%) rotate(${angle}rad);
            z-index: 9999;
            pointer-events: none;
            animation: muzzleFlash 0.1s ease-out;
        `;
        
        document.body.appendChild(flash);
        
        // 添加开火动画样式
        if (!document.getElementById('muzzleFlashStyles')) {
            const styles = document.createElement('style');
            styles.id = 'muzzleFlashStyles';
            styles.textContent = `
                @keyframes muzzleFlash {
                    0% { 
                        opacity: 1;
                        transform: translate(-50%, -50%) rotate(${angle}rad) scale(0.5);
                    }
                    100% { 
                        opacity: 0;
                        transform: translate(-50%, -50%) rotate(${angle}rad) scale(2);
                    }
                }
            `;
            document.head.appendChild(styles);
        }
        
        // 移除闪光效果
        setTimeout(() => {
            if (flash.parentNode) {
                document.body.removeChild(flash);
            }
        }, 100);
    }
    
    // 渲染所有效果
    renderAll(ctx) {
        this.renderEnhancedParticles(ctx);
        this.renderDamageNumbers(ctx);
        this.renderKillStreak(ctx);
    }
    
    renderKillStreak(ctx) {
        if (this.killStreak > 1) {
            ctx.save();
            ctx.fillStyle = '#ff6b6b';
            ctx.font = 'bold 24px Arial';
            ctx.textAlign = 'center';
            ctx.strokeStyle = '#000';
            ctx.lineWidth = 2;
            
            const text = `${this.killStreak}x STREAK!`;
            const x = this.canvas.width / 2;
            const y = 80;
            
            ctx.strokeText(text, x, y);
            ctx.fillText(text, x, y);
            ctx.restore();
        }
    }
    
    // 重置系统
    reset() {
        this.killStreak = 0;
        this.lastKillTime = 0;
        this.damageNumbers = [];
        this.enhancedParticles = [];
        this.screenShake = {
            intensity: 0,
            duration: 0,
            offsetX: 0,
            offsetY: 0
        };
        this.slowMotion = {
            active: false,
            factor: 1,
            duration: 0
        };
        
        // 清除所有动态特效元素
        this.clearDynamicEffects();
    }
    
    // 清除动态特效元素
    clearDynamicEffects() {
        // 清除所有动态创建的特效元素
        const effectElements = document.querySelectorAll('.kill-effect-text, .damage-effect-text, .damage-ripple, .muzzle-flash');
        effectElements.forEach(element => {
            if (element.parentNode) {
                element.parentNode.removeChild(element);
            }
        });
    }
    
    // 添加特效管理方法
    createTimedEffect(element, duration) {
        document.body.appendChild(element);
        
        setTimeout(() => {
            if (element.parentNode) {
                element.parentNode.removeChild(element);
            }
        }, duration);
    }
    
    // 添加特效对象池优化
    getEffectFromPool(type) {
        // 这里可以实现对象池优化，避免频繁创建和销毁DOM元素
        return null; // 简化实现
    }
    
    // 添加特效质量设置
    setEffectQuality(quality) {
        this.effectQuality = quality; // 'low', 'medium', 'high'
        
        switch(quality) {
            case 'low':
                this.maxParticles = 50;
                this.particleLife = 0.5;
                break;
            case 'medium':
                this.maxParticles = 100;
                this.particleLife = 1.0;
                break;
            case 'high':
                this.maxParticles = 200;
                this.particleLife = 1.5;
                break;
        }
    }
    
    // 新增：僵尸技能特效
    createZombieDeathEffect(x, y, level, type) {
        const intensity = level;
        const particleCount = 15 + intensity * 5;
        
        for (let i = 0; i < particleCount; i++) {
            this.enhancedParticles.push({
                type: 'zombieDeath',
                x: x + (Math.random() - 0.5) * 30,
                y: y + (Math.random() - 0.5) * 30,
                vx: (Math.random() - 0.5) * 200,
                vy: (Math.random() - 0.5) * 200 - 50,
                size: Math.random() * (3 + intensity) + 2,
                color: this.getZombieDeathColor(type, level),
                life: 1500 + intensity * 300,
                maxLife: 1500 + intensity * 300,
                rotation: Math.random() * Math.PI * 2,
                rotationSpeed: (Math.random() - 0.5) * 5
            });
        }
        
        // 高等级僵尸死亡时的屏幕震动 - 减少强度
        if (level >= 3) {
            // this.startScreenShake(Math.min(level, 4), 200); // 禁用高等级僵尸震动
        }
    }
    
    getZombieDeathColor(type, level) {
        const baseColors = {
            'normal': '#8B0000',
            'fast': '#FF4500',
            'tank': '#696969',
            'spitter': '#9ACD32'
        };
        
        const baseColor = baseColors[type] || '#8B0000';
        
        // 高等级僵尸有更亮的颜色
        if (level >= 4) {
            return '#FFD700'; // 金色
        } else if (level >= 3) {
            return '#FF6347'; // 橙红色
        }
        
        return baseColor;
    }
    
    createHealEffect(x, y) {
        const healEffect = document.createElement('div');
        healEffect.style.cssText = `
            position: fixed;
            left: ${x - 25}px;
            top: ${y - 25}px;
            width: 50px;
            height: 50px;
            border: 3px solid #32CD32;
            border-radius: 50%;
            background: radial-gradient(circle, rgba(50, 205, 50, 0.3), transparent);
            z-index: 9999;
            pointer-events: none;
            animation: healPulse 1s ease-out;
        `;
        
        // 添加治疗动画样式
        if (!document.getElementById('healEffectStyles')) {
            const styles = document.createElement('style');
            styles.id = 'healEffectStyles';
            styles.textContent = `
                @keyframes healPulse {
                    0% { transform: scale(0.5); opacity: 1; }
                    50% { transform: scale(1.2); opacity: 0.7; }
                    100% { transform: scale(2); opacity: 0; }
                }
            `;
            document.head.appendChild(styles);
        }
        
        this.createTimedEffect(healEffect, 1000);
        
        // 添加治疗粒子效果
        for (let i = 0; i < 12; i++) {
            this.enhancedParticles.push({
                type: 'heal',
                x: x,
                y: y,
                vx: (Math.random() - 0.5) * 50,
                vy: -Math.random() * 100 - 50,
                size: Math.random() * 4 + 2,
                color: '#32CD32',
                life: 1500,
                maxLife: 1500,
                rotation: 0,
                rotationSpeed: 0
            });
        }
    }
    
    createExplosionEffect(x, y, radius) {
        // 彻底禁用爆炸冲击波效果 - 这是造成红色圆圈的主要源头！
        return;
        /*
        // 创建爆炸冲击波
        const explosion = document.createElement('div');
        explosion.style.cssText = `
            position: fixed;
            left: ${x - radius}px;
            top: ${y - radius}px;
            width: ${radius * 2}px;
            height: ${radius * 2}px;
            border: 4px solid #FF4500;
            border-radius: 50%;
            background: radial-gradient(circle, rgba(255, 69, 0, 0.6), rgba(255, 140, 0, 0.3), transparent);
            z-index: 9999;
            pointer-events: none;
            animation: explosionBlast 0.8s ease-out;
        `;
        */
        
        // 添加爆炸动画样式
        if (!document.getElementById('explosionEffectStyles')) {
            const styles = document.createElement('style');
            styles.id = 'explosionEffectStyles';
            styles.textContent = `
                @keyframes explosionBlast {
                    0% { transform: scale(0.1); opacity: 1; }
                    30% { transform: scale(0.8); opacity: 0.9; }
                    100% { transform: scale(1.5); opacity: 0; }
                }
            `;
            document.head.appendChild(styles);
        }
        
        this.createTimedEffect(explosion, 800);
        
        // 爆炸粒子效果
        const particleCount = Math.floor(radius / 3);
        for (let i = 0; i < particleCount; i++) {
            const angle = (Math.PI * 2 * i) / particleCount;
            const speed = 150 + Math.random() * 100;
            
            this.enhancedParticles.push({
                type: 'explosion',
                x: x,
                y: y,
                vx: Math.cos(angle) * speed,
                vy: Math.sin(angle) * speed,
                size: Math.random() * 6 + 3,
                color: ['#FF4500', '#FF6347', '#FFD700'][Math.floor(Math.random() * 3)],
                life: 1000 + Math.random() * 500,
                maxLife: 1000,
                rotation: Math.random() * Math.PI * 2,
                rotationSpeed: (Math.random() - 0.5) * 10
            });
        }
        
        // 强烈屏幕震动
        // this.startScreenShake(15, 600); // 禁用强烈震动
    }
}

// 全局实例
window.visualEffects = new VisualEffectsSystem();
window.visualEffectsSystem = window.visualEffects; // 别名支持

// 初始化特效质量
window.visualEffects.setEffectQuality('high');

console.log('视觉效果系统已加载 - 增强版 + 僵尸技能特效');